﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
namespace NASM_Develop_IDE
{

    public class Solution
    {
        private string absolutePathOfBinFolder;
        private List<string> absolutePathOfSources;
        private List<string> absolutePathOfObjectsFiles;
        private string outputName;
        public string NameOfOutputBinary
        {
            set
            {
                outputName = value;
            }
            get
            {
                return outputName;
            }
        }
        public List<string> AbsolutePathOfSources
        {
            set
            {
                absolutePathOfSources = value;
            }
            get
            {
                return absolutePathOfSources;
            }
        }
        public string AbsolutePathOfBinFolder
        {
            set
            {
                absolutePathOfBinFolder = value;
            }
            get
            {
                return absolutePathOfBinFolder;
            }
        }
        [Obsolete("Although created for keeping track of object files. It is not necessary since the path of bin folder is just needed.", false)]
        public List<string> AbsolutePathOfObjectsFiles
        {
            set
            {
               
                absolutePathOfObjectsFiles = value;
            }
            get
            {
                return absolutePathOfObjectsFiles;
            }
        }
        public Solution() { return; }
        public static bool CreateSolution(string path, Solution s) 
        {

            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Solution));
                {
                    using (Stream stream = File.Open(path, FileMode.Create))
                    {
                        serializer.Serialize(stream, s);
                    }
                    serializer = null;
                    GC.Collect();

                }
                return true;
            }
            catch (Exception e)
            {
                ExceptionPrinter.Print(e);
                return false;
            }
        }
        public static Solution GetSolution(string path)
        {
            try
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Solution));
                object o;
                using (FileStream s = new FileStream(path, FileMode.Open))
                {
                    o = serializer.Deserialize(s);
                    
                }
                serializer = null;
                GC.Collect();
                return o as Solution;
            }
            catch (Exception e)
            {
                ExceptionPrinter.Print(e);
                return null;
            }
        }
        private bool UpdateSolution()
        {
            try
            {
                DirectoryInfo info = new DirectoryInfo(Path.GetDirectoryName(this.AbsolutePathOfSources[0]));
                string path = info.Parent.GetFiles("*.nasmproj")[0].FullName;
                return CreateSolution(path, this);
            }
            catch
            {
                return false;
            }
        }
        public bool Save(NasmEditTabList list)
        {
            try
            {
                UpdateSolution();
                Dictionary<ScintillaNET.Scintilla, string> tabs = list.Tabs;
                list.Foreach(delegate(ScintillaNET.Scintilla obj)
                {
                    string path = tabs[obj]; // Gets The Path Associated With Tab
                    
                    using (Stream stream = File.Open(path, FileMode.Create))
                    {
                        using (StreamWriter writer = new StreamWriter(stream))
                        {
                            writer.Write(obj.Text);
                        }
                    }
                });
                return true;
            }
            catch (Exception e)
            {
                ExceptionPrinter.Print(e);
                return false;
            }
        }
        public static bool operator==(Solution a, Solution b)
        {

                //x and x2 are here to check if x or x2 are null. Without them a nasty bug occurs
                // when comparing null references.
                object x = a; 
                object x2 = b;
                if (x == null && x2 == null)
                {
                    return true;
                }
                else if (x == null || x2 == null)
                {
                    return false;
                }
                return (a.AbsolutePathOfBinFolder == b.AbsolutePathOfBinFolder && ListHelper.CompareList<string>(a.AbsolutePathOfSources, b.AbsolutePathOfSources));
        }
       
        public static bool operator !=(Solution a, Solution b)
        {
            return !(a == b);
        }
        public override bool Equals(object obj)
        {
            return ((obj as Solution) == this);
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public override string ToString()
        {
            return "XML Based Solution";
        }
        
    }
}
